﻿#include "tframework.h"

#include "../../include/itlogger.h"
#include "../../include/itcommon.h"
#include "../../include/ITConfig.h"
#include "../TPublicMgr/tpublicmgr.h"
#include "../TUiMgr/tuimgr.h"
#include "../TService/tservice.h"

extern "C" __declspec(dllexport) ITFramework* GetInstance()
{
    return dynamic_cast<ITFramework*>(TFramework::GetInstance());
}

TFramework::TFramework()
{
    m_pLibConfig    = NULL;
    m_pLibCommon    = NULL;
    m_pLogger       = NULL;
    m_pCommon       = NULL;
    m_pConfig       = NULL;
    m_pPublicMgr    = NULL;
    m_pUiMgr        = NULL;
    m_pSalesman     = NULL;
    m_pSaleswoman   = NULL;
    m_pUiService    = NULL;
    m_pfnCBStart    = NULL;
    m_pUserdataStart= NULL;
    m_pfnCBCheckIn  = NULL;
    m_pUserdataCheckIn= NULL;
    m_pfnCBConsultant= NULL;
    m_pUserdataConsultant= NULL;
    m_pfnCBGuide    = NULL;
    m_pUserdataGuide= NULL;
    m_pfnCBPropagandist= NULL;
    m_pUserdataPropagandist= NULL;
}

TFramework::~TFramework()
{

}

TRESULT TFramework::Init()
{
    m_pLogger->WriteLog("TFramework::Init()");
    TRESULT hr = T_FALSE;
    hr = InitConfig();
    if(FAILEDT(hr))
        return hr;

    hr = InitCommon();
    if(FAILEDT(hr))
        return hr;

    m_pSalesman = TmSalesman::GetInstance();
    m_pSalesman->SetFramework(this);
    hr = m_pSalesman->Init();
    if(FAILEDT(hr))
        return hr;

    m_pSaleswoman = TmSaleswoman::GetInstance();
    m_pSaleswoman->SetFramework(this);
    hr = m_pSaleswoman->Init();
    if(FAILEDT(hr))
        return hr;

    m_pUiService = TmUiService::GetInstance();
    m_pUiService->SetFramework(this);
    hr = m_pUiService->Init();
    if(FAILEDT(hr))
        return hr;

    m_pPublicMgr = TPublicMgr::GetInstance();
    m_pPublicMgr->SetFramework(this);
    hr = m_pPublicMgr->Init();
    if(FAILEDT(hr))
        return hr;

    m_pUiMgr = TUiMgr::GetInstance();
    m_pUiMgr->SetFramework(this);
    hr = m_pUiMgr->Init();
    if(FAILEDT(hr))
        return hr;

    return T_OK;
}

TRESULT TFramework::UnInit()
{
    qDebug() << "TFramework::UnInit()";
    m_pSalesman->UnInit();
    m_pSaleswoman->UnInit();
    m_pUiService->UnInit();
    m_pPublicMgr->UnInit();
    m_pUiMgr->UnInit();
    UnInitConfig();
    UnInitCommon();
    return T_OK;
}

TRESULT TFramework::SetLogger(ITLogger *pLogger)
{
    m_pLogger = pLogger;
    return T_OK;
}

TRESULT TFramework::GetLogger(ITLogger **ppLogger)
{
    *ppLogger = m_pLogger;
    return T_OK;
}

TRESULT TFramework::GetPublicMgr(ITPublicMgr **ppMgr)
{
    *ppMgr = m_pPublicMgr;
    return T_OK;
}

TRESULT TFramework::GetUiMgr(ITUiMgr **ppMgr)
{
    *ppMgr = m_pUiMgr;
    return T_OK;
}

TRESULT TFramework::GetSalesman(ITService **ppService)
{
    *ppService = m_pSalesman;
    return T_OK;
}

TRESULT TFramework::GetSaleswoman(ITService **ppService)
{
    *ppService = m_pSaleswoman;
    return T_OK;
}

TRESULT TFramework::GetUiService(ITService **ppService)
{
    *ppService = m_pUiService;
    return T_OK;
}

TRESULT TFramework::GetCommon(ITCommon **ppCommon)
{
    *ppCommon = m_pCommon;
    return T_OK;
}

TRESULT TFramework::SetCBDlgStart(pfnCBClient pfn, void *pUserdata)
{
    m_pfnCBStart = pfn;
    m_pUserdataStart = pUserdata;
    return T_OK;
}

TRESULT TFramework::GetCBDlgStart(pfnCBClient *ppfn, void **ppUserdata)
{
    *ppfn = m_pfnCBStart;
    *ppUserdata = m_pUserdataStart;
    return T_OK;
}

TRESULT TFramework::SetCBCheckIn(pfnCBClient pfn, void *pUserdata)
{
    m_pfnCBCheckIn = pfn;
    m_pUserdataCheckIn = pUserdata;
    return T_OK;
}

TRESULT TFramework::GetCBCheckIn(pfnCBClient *ppfn, void **ppUserdata)
{
    *ppfn = m_pfnCBCheckIn;
    *ppUserdata = m_pUserdataCheckIn;
    return T_OK;
}

TRESULT TFramework::SetCBConsultant(pfnCBClient pfn, void *pUserdata)
{
    m_pfnCBConsultant = pfn;
    m_pUserdataConsultant = pUserdata;
    return T_OK;
}

TRESULT TFramework::GetCBConsultant(pfnCBClient *ppfn, void **ppUserdata)
{
    *ppfn = m_pfnCBConsultant;
    *ppUserdata = m_pUserdataConsultant;
    return T_OK;
}

TRESULT TFramework::SetCBGuide(pfnCBClient pfn, void *pUserdata)
{
    m_pfnCBGuide = pfn;
    m_pUserdataGuide = pUserdata;
    return T_OK;
}

TRESULT TFramework::GetCBGuide(pfnCBClient *ppfn, void **ppUserdata)
{
    *ppfn = m_pfnCBGuide;
    *ppUserdata = m_pUserdataGuide;
    return T_OK;
}

TRESULT TFramework::SetCBPropagandist(pfnCBClient pfn, void *pUserdata)
{
    m_pfnCBPropagandist = pfn;
    m_pUserdataPropagandist = pUserdata;
    return T_OK;
}

TRESULT TFramework::GetCBPropagandist(pfnCBClient *ppfn, void **ppUserdata)
{
    *ppfn = m_pfnCBPropagandist;
    *ppUserdata = m_pUserdataPropagandist;
    return T_OK;
}

TRESULT TFramework::InitConfig()
{
    //load TmConfig.dll
    TRESULT hr;
    QString strCfgPath("module/Config.dll");
    m_pLibConfig = new QLibrary(strCfgPath);
    if(m_pLibConfig->load())
    {
        typedef ITConfig* (*GetInstance)();
        GetInstance pGetInstance = (GetInstance)m_pLibConfig->resolve("GetInstance");
        if(pGetInstance)
            m_pConfig = pGetInstance();
    }
    if(NULL != m_pConfig)
    {
        m_pConfig->SetFramework(this);
        hr = m_pConfig->Init();
    }
    return hr;
}

void TFramework::UnInitConfig()
{
    if(m_pLibConfig && m_pLibConfig->isLoaded())
    {
        m_pConfig->UnInit();
        m_pLibConfig->unload();
        qDebug() << "Tami unload Config";
    }
    SAFE_DELETE(m_pLibConfig);
}

TRESULT TFramework::InitCommon()
{
    QString strpath = QString("module/Common.dll");
    m_pLibCommon = new QLibrary(strpath);
    if(m_pLibCommon->load())
    {
        typedef ITCommon* (*GetInstance)();
        GetInstance pGetInstance = (GetInstance)m_pLibCommon->resolve("GetInstance");
        if(pGetInstance)
            m_pCommon = pGetInstance();
        if(NULL != m_pCommon)
        {
            m_pCommon->SetFramework(this);
            return T_OK;
        }
    }
    return T_FALSE;
}

void TFramework::UnInitCommon()
{
    if(m_pLibCommon && m_pLibCommon->isLoaded())
    {
        m_pLibCommon->unload();
        qDebug() << "Tami unload Common";
    }
    SAFE_DELETE(m_pLibCommon);
}
